<?php

class Ext_Db extends Zend_Db 
{
    // Copiado rigidamente de Zend_Db adapter. Solo cambio $adapterNamespace
    public static function factory( $adapter, $config = array() ) 
    {
        if( $config instanceof Zend_Config ) {
            $config = $config->toArray();
        }
        /*
         * Convert Zend_Config argument to plain string
         * adapter name and separate config object.
         */
        if( $adapter instanceof Zend_Config ) {
            if( isset( $adapter->params ) ) {
                $config = $adapter->params->toArray();
            }
            if( isset( $adapter->adapter ) ) {
                $adapter = ( string ) $adapter->adapter;
            } else {
                $adapter = null;
            }
        }
        /*
         * Verify that adapter parameters are in an array.
         */
        if( ! is_array( $config ) ) {
            /**
             * @see Zend_Db_Exception
             */
            require_once 'Zend/Db/Exception.php';
            throw new Zend_Db_Exception( 'Adapter parameters must be in an array or a Zend_Config object' );
        }
        /*
         * Verify that an adapter name has been specified.
         */
        if( ! is_string( $adapter ) || empty( $adapter ) ) {
            /**
             * @see Zend_Db_Exception
             */
            require_once 'Zend/Db/Exception.php';
            throw new Zend_Db_Exception( 'Adapter name must be specified in a string' );
        }
        /*
         * Form full adapter class name
         */
        $adapterNamespace = 'Ext_Db_Adapter';
        if( isset( $config[ 'adapterNamespace' ] ) ) {
            if( $config[ 'adapterNamespace' ] != '' ) {
                $adapterNamespace = $config[ 'adapterNamespace' ];
            }
            unset( $config[ 'adapterNamespace' ] );
        }
        $adapterName = strtolower( $adapterNamespace . '_' . $adapter );
        $adapterName = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $adapterName ) ) );
        
        /*
         * Load the adapter class.  This throws an exception
         * if the specified class cannot be loaded.
         */
        @Zend_Loader::loadClass( $adapterName );
        
        /*
         * Create an instance of the adapter class.
         * Pass the config to the adapter class constructor.
         */
        $dbAdapter = new $adapterName( $config );
        /*
         * Verify that the object created is a descendent of the abstract adapter type.
         */
        if( ! $dbAdapter instanceof Zend_Db_Adapter_Abstract ) {
            /**
             * @see Zend_Db_Exception
             */
            require_once 'Zend/Db/Exception.php';
            throw new Zend_Db_Exception( "Adapter class '$adapterName' does not extend Zend_Db_Adapter_Abstract" );
        }
        
        return $dbAdapter;
    }
}